<template>
  <div>
    <!-- 产品详情{{ $route.query.id }} -->

    <!-- 轮播图组件 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" style="dispaly:block" />
      </van-swipe-item>
    </van-swipe>

    <!-- 提示组件（自定义） -->
    <Tips />

    <!-- 商品信息 -->
    <div class="info">
      <div class="title">{{ info.name }}</div>
      <div class="goods_brief">{{ info.goods_brief }}</div>
      <div class="retail_price">{{ info.retail_price | RMBformat }}</div>
    </div>

    <!-- cell组件: 展示弹出层 箭头 -->
    <van-cell title="展示弹出层" is-link @click="isSkuShow = true" />
    <!-- 商品参数 -->
    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, key) in attribute" :key="key">
          <div class="l">{{ item.name }}</div>
          <div class="r van-ellipsis">{{ item.value }}</div>
        </li>
      </ul>
    </div>

    <!-- 商品信息（大量图片信息） -->
    <div v-html="goods_desc" class="goods_desc"></div>

    <!-- 标题结构样式 -->
    <div class="title">
      <span>常见问题</span>
    </div>

    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>

    <div class="title">
      <span>大家都在看</span>
    </div>

    <Products :goodsList="goodsList" />

    <!-- 商品规格 -->
    <van-sku
      v-model="isSkuShow"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />

    <!-- 商品导航 -->
    <AppGoodsActive />
  </div>
</template>

<script>
import { GetGoodsDetailData, GetGoodsRelatedData } from '@/request/api'
import Tips from '@/components/Tips'
import Products from '@/components/Products'
import AppGoodsActive from '@/components/AppGoodsActive'

export default {
  data() {
    return {
      // 商品规格
      isSkuShow: false,
      sku: {
        tree: [],
        hide_stock: false,
        price: '1.00', // 默认价格（单位元）
        stock_num: 227, // 商品总库存
      },
      goods: {},

      // 轮播图数据
      gallery: [],
      // 商品信息
      info: {},
      // 商品参数
      attribute: [],
      // 商品信息（大量图片信息）
      goods_desc: '',
      // 常见问题
      issue: [],
      // 相关产品列表
      goodsList: [],
    }
  },
  components: {
    Tips,
    AppGoodsActive,
    Products,
  },
  created() {
    // 发送请求：请求这个产品的详情数据
    GetGoodsDetailData({
      id: this.$route.query.id,
    }).then((res) => {
      console.log(res.data)
      // 普通写法
      // this.gallery = res.data.gallery

      // 解构写法
      let { gallery, info, attribute, issue } = res.data
      this.gallery = gallery
      this.info = info
      this.attribute = attribute
      this.goods_desc = info.goods_desc
      this.issue = issue

      // sku数据
      this.goods.picture = info.list_pic_url
      this.sku.price = info.retail_price
      this.sku.stock_num = info.goods_number
    })
    // 发送请求：获取相关产品数据（大家都在看模块）
    GetGoodsRelatedData({
      id: this.$route.query.id,
    }).then((res) => {
      console.log(res.data)
      this.goodsList = res.data.goodsList
    })
  },
}
</script>

<style lang="less" scoped>
.info {
  background-color: #fff;
  text-align: center;
  line-height: 0.4rem;
  padding: 0.1rem 0;
  border-bottom: 1px solid #ccc;
  .title {
    font-size: 0.2rem;
  }
  .retail_price {
    color: darkred;
  }
}
.attribute {
  background-color: #fff;
  margin-top: 0.2rem;
  padding: 0.1rem 0.168rem;
  h3 {
    font-size: 0.1638rem;
    line-height: 0.5rem;
  }
  li {
    height: 0.4rem;
    line-height: 0.4rem;
    background-color: #efefef;
    display: flex;
    margin-bottom: 0.1rem;
    border-radius: 0.5rem;
    .l {
      width: 30%;
      text-align: right;
      margin-right: 0.1rem;
      font-size: 0.12rem;
    }
    .r {
      flex: 1;
      font-size: 0.168rem;
    }
  }
}
// 深度样式写法，穿透UI组件的样式，直接添加在标签上
/deep/.goods_desc {
  img {
    display: block;
    width: 100%;
  }
}

.title {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: '';
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}

.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: '';
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
